import UIKit

//数组
var IntArray = [Int]()
IntArray.append(3)
IntArray.append(10)
IntArray = []
var someInts = [Any]()
someInts.append("lin")
someInts.append(10.33)
someInts.append(10)
//将数组置为空
someInts = []

//默认值创建数组
var threeDouble = Array(repeating: 1.5, count: 5)
//只能新增添加同类型的
//threeDouble.append("app")
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
//相加只能是数组里面类型一致的
var mixArray = anotherThreeDoubles + threeDouble

//var shoppingList: [String] = ["Eggs", "Milk"]
var shoppingList = ["Eggs", "Milk"]
print("count为只读属性\(shoppingList.count)")
if shoppingList.isEmpty {
    print("数组的属性isEmpty只是用来检查数组的count是否为0")
}else{
    print("数组count不为0")
}
print("append用法和字符串相同，但是元素类型必须一致\(shoppingList.append("Flour"))")
shoppingList
//可以通过运算符新增一个元素 +=
shoppingList += ["cake"]
shoppingList += ["chocolate","beer"]
//通过下标直接修改array的值
shoppingList[0] = "six eggs"
shoppingList
//insert 和  remove
shoppingList.insert("juice", at: 1)
shoppingList.remove(at: 1)
shoppingList
shoppingList.removeLast()
shoppingList
shoppingList.remove(at: shoppingList.count-1)
shoppingList
//遍历

for item in shoppingList {
    print("\(item)\n")
}
//下面方法可以遍历到数组的值和元素的索引  enumerated 可以遍历每个元素的元组
for (index,value) in shoppingList.enumerated() {
    print("index:\(index).value:\(value)\n")
}

//合集 --- 同一类型 无序 不重复 存储到一个合集中
var letters = Set<Character>()
letters.insert("a")
letters
letters = []

//var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
//简写
var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]

//访问和修改
if favoriteGenres.isEmpty {
    print("检查count是否为0")
}
//增加一个叫jazz的元素
favoriteGenres.insert("Jazz")
if let removeGenre = favoriteGenres.remove("Rock"){
    print("remove会返回被删除的元素\(removeGenre)")
}else{
    print("如果没有值则会返回nil")
}
if favoriteGenres.contains("funk") {
    print("contains")
}else{
    print("not contains")
}
//遍历
for genre in favoriteGenres{
    print(genre)
}

//排序  sorted() 默认升序  sorted(by:<) 升序   sorted(by:>)降序
for genre in favoriteGenres.sorted() {
    print("sorted() 默认升序:\(genre)")
}
for genre in favoriteGenres.sorted(by:<) {
    print("sorted(by:<) 升序:\(genre)")
}
for genre in favoriteGenres.sorted(by:>) {
    print("sorted(by:>) 降序:\(genre)")
}

//基本合集操作
let oddDigits:Set = [1,3,5,7,9]
let evenDigits:Set = [0,2,4,6,8]
let singleDigitPrimeNumbers: Set = [2, 3, 5, 7]

var newDigits = Set<Int>()

// union 创建一个两个合集所有值的新合集  不会有重复元素
newDigits = oddDigits.union(evenDigits)
newDigits.sorted()
print(newDigits.sorted())
newDigits = []

//intersection 创建一个 两个合集共有值的新合集
newDigits = oddDigits.intersection(evenDigits)
newDigits
newDigits = oddDigits.intersection(singleDigitPrimeNumbers)
newDigits = []

//subtracting 创建一个  a合集不包含b合集元素（a-b=c）的新合集（c）--差异值
newDigits = oddDigits.subtracting(singleDigitPrimeNumbers)
print(newDigits.sorted())
newDigits = []

//symmetricDifference 创建一个  除去两个合集相同元素 剩余元素的新合集
newDigits = oddDigits.symmetricDifference(singleDigitPrimeNumbers)

//合集成员关系和相等性
let houseAnimals: Set = ["🐶", "🐱"]
let farmAnimals: Set = ["🐶", "🐱" ,"🐮", "🐔", "🐑"]
let cityAnimals: Set = ["🐦", "🐭"]
//farmAnimals 是 houseAnimals 的超集 反之  houseAnimals是farmAnimals的子集
//因为farmAnimals 包含 houseAnimals的所有值

//isSubset  a是否是b子集
houseAnimals.isSubset(of: farmAnimals)
//isSuperset a是否是b的超集
farmAnimals.isSuperset(of: houseAnimals)
//isDisjoint a和b是否有完全不相同的元素
houseAnimals.isDisjoint(with: cityAnimals)



//字典
//储存无序的互相关联的同一类型的键和同一类型的值的集合。
var namesOfIntegers = [Int:String]()
//可以直接赋值
namesOfIntegers[16] = "sixteen"
namesOfIntegers
//赋值为空
namesOfIntegers = [:]
namesOfIntegers

//var airports:[String:String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
//简写
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]

print("The airports dictionary contains \(airports.count) items.")
//isEmpty
if airports.isEmpty {
    print("count为0")
}else{
    print("count不为0")
}
//赋值新加键值队 也可通过key直接修改值
airports["LHR"] = "London Heathrow"
airports
airports["LHR"] = "London"
airports

//uopdateValue(_:forKey:)  更新值  如果无值 -> 新增  如果有值->更新原有值
//uopdateValue(_:forKey:)  返回值是旧值 所以如果之前无值  那么将返回的是nil⚠️
if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
    print("The old value for DUB was \(oldValue).")
}
airports
//因为之前没有这key：value  返回的是原来的值  所以返回的是nil  所以不会执行打印操作
//但是已经添加成功了 ⚠️
if let oldValue = airports.updateValue("DDA", forKey: "XXY") {
    print("The old value was \(oldValue).")
}
airports
//也可用这种方法查找
if let airportName = airports["DUB"] {
    print("The name of the airport is \(airportName).")
} else {
    print("That airport is not in the airports dictionary.")
}

//移除键值对
airports["XXY"] = nil
airports
//也可以用这个方法 和update方法是一样的  返回的也是旧的键值对
if let removedValue = airports.removeValue(forKey: "DUB") {
    print("The removed airport's name is \(removedValue).")
} else {
    print("The airports dictionary does not contain a value for DUB.")
}
// Prints "The removed airport's name is Dublin Airport."


//遍历
for (key,value) in airports {
    print("key:\(key).value:\(value)")
}
//单独遍历key或是value
for key in airports.keys {
    print("keys:\(key)")
}
for value in airports.values {
    print("value:\(value)")
}

//可以用这样来接收key
let airportKeyArray = [String](airports.keys)
let airportValueArray = [String](airports.values)

print("\(airports.keys)")
print("\(airports.values)")
airports.keys
airports.values
